package com.trkj.service.wdm;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.trkj.dao.wdm.CharacteristicItemDAO;
import com.trkj.dao.wdm.DefinitionDeptDao;
import com.trkj.dao.wdm.DutyDAO;
import com.trkj.dao.wdm.EdubackgroundsDAO;
import com.trkj.dao.wdm.EmpPassDAO;
import com.trkj.dao.wdm.JobStatusDAO;
import com.trkj.dao.wdm.LaborTurnoverDao;
import com.trkj.dao.wdm.MaritalStatusDAO;
import com.trkj.dao.wdm.MotherTongueDAO;
import com.trkj.dao.wdm.NationalityDAO;
import com.trkj.dao.wdm.ProfessionalTitleDAO;
import com.trkj.dao.wdm.ReasonsLeavingDAO;
import com.trkj.dao.wdm.StaffInformationDAO;
import com.trkj.mybatisdao.LaboturnMapper;
import com.trkj.mybatisdao.StaffinformationDAOI;
import com.trkj.mybatisdao.XiuGaiMMDAO;
import com.trkj.pojo.DefinitionDept;
import com.trkj.pojo.Edubackground;
import com.trkj.pojo.Emppass;
import com.trkj.pojo.JobStatus;
import com.trkj.pojo.LaborTurnover;
import com.trkj.pojo.ProfessionalTitle;
import com.trkj.pojo.ReasonsLeaving;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.vo.ShouYeVo;
import com.trkj.vo.StaffMessage;
import com.trkj.vo.StaffVO;

@Service
@Transactional(rollbackFor = Exception.class)
public class StaffService {
	@Autowired
	private StaffInformationDAO sdao;
	@Autowired
	private DefinitionDeptDao ddao;
	@Autowired
	private JobStatusDAO jdao;
	@Autowired
	private DutyDAO dutydao;
	@Autowired
	private ProfessionalTitleDAO pdao;
	@Autowired
	private EdubackgroundsDAO edao;
	@Autowired
	private MaritalStatusDAO mdao;
	@Autowired
	private NationalityDAO ndao;
	@Autowired
	private MotherTongueDAO motherdao;
	@Autowired
	private CharacteristicItemDAO cdao;
	@Autowired
	private EmpPassDAO emoDao;
	@Autowired
	private LaborTurnoverDao laborDao;
	@Autowired
	private ReasonsLeavingDAO reDao;
	@Autowired
	private LaboturnMapper laborMapper;
	@Autowired
	private StaffinformationDAOI sssd;
	@Autowired
	private XiuGaiMMDAO xgmm;
	
	
	public void addStaff(StaffMessage s) throws ServiceException{
		try {
			StaffInformation staff = new StaffInformation();
			staff.setStainName(s.getStain_name());
			staff.setStainIdentity(s.getStain_identity());
			staff.setStainSex(s.getStain_sex());
			staff.setStainBirthday(s.getStain_bir());
			staff.setStainDate(s.getStain_date());
			staff.setStainEstimatedate(s.getStain_estimatedate());
			staff.setStainPolitics(s.getStain_politics());
			staff.setStainSrc(s.getStain_src());
			staff.setDefinitionDept(ddao.findOne(s.getDeptId()));
			staff.setDuty(dutydao.findOne(s.getDutyId()));
			staff.setJobStatus(jdao.findOne(s.getJobsId()));
			staff.setProfessionalTitle(pdao.findOne(s.getZcId()));
			staff.setEdubackground(edao.findOne(s.getEduId()));
			staff.setNationality(ndao.findOne(s.getMinzuId()));
			staff.setMaritalStatus(mdao.findOne(s.getHunyinId()));
			staff.setMotherTongue(motherdao.findOne(s.getMuyuId()));
			staff.setCharacteristicItem(cdao.findOne(s.getTzx()));
			
			Emppass e = new Emppass();
			e.setPassPwd("123456");
			e.setStaffInformation(staff);
			
			LaborTurnover la = new LaborTurnover();
			la.setStaffInformation(staff);
			la.setVoluntaryTurnover(new BigDecimal(0));
			
			sdao.save(staff);
			emoDao.save(e);
			laborDao.save(la);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	
	//修改密码
	public void xiugaimima (Long id,String jmm,String xmm){
		JSONObject emp =xgmm.mmcx(id);
		
		if(jmm.equals(emp.get("PASS_PWD"))){
			
			Object a = emp.get("PASS_ID");
			long l = Long.valueOf(String.valueOf(a)).longValue();
			Emppass emp22 = emoDao.findOne(l);
			emp22.setPassPwd(xmm);
			emoDao.save(emp22);
		}else{
			System.out.println("錯誤");
		}
		

		
		
	}
	
	/**
	  * 修改员工信息
	 * @param stain_name 姓名
	 * @param stain_identity 身份证
	 * @param stain_sex	性别
	 * @param stain_bir	生日
	 * @param stain_date 入职日期
	 * @param stain_estimatedate	转正时间
	 * @param stain_politics	政治面貌
	 * @param stain_src 头像
	 * @param deptId	部门
	 * @param dutyId	职务
	 * @param jobsId	用工状态
	 * @param zcId	职称
	 * @param eduId	学历
	 * @param minzuId 民族
	 * @param hunyinId	婚姻
	 * @param muyuId	母语
	 * @param tzx	特征项
	 * @throws ServiceException
	 */
	public void updateStaff(StaffMessage s) throws ServiceException{
		try {
			StaffInformation staff = sdao.findOne(s.getStain_id());
			staff.setStainName(s.getStain_name());
			staff.setStainIdentity(s.getStain_identity());
			staff.setStainSex(s.getStain_sex());
			staff.setStainBirthday(s.getStain_bir());
			staff.setStainDate(s.getStain_date());
			staff.setStainEstimatedate(s.getStain_estimatedate());
			staff.setStainPolitics(s.getStain_politics());
			staff.setStainSrc(s.getStain_src());
			staff.setDefinitionDept(ddao.findOne(s.getDeptId()));
			staff.setDuty(dutydao.findOne(s.getDutyId()));
			staff.setJobStatus(jdao.findOne(s.getJobsId()));
			staff.setProfessionalTitle(pdao.findOne(s.getZcId()));
			staff.setEdubackground(edao.findOne(s.getEduId()));
			staff.setNationality(ndao.findOne(s.getMinzuId()));
			staff.setMaritalStatus(mdao.findOne(s.getHunyinId()));
			staff.setMotherTongue(motherdao.findOne(s.getMuyuId()));
			staff.setCharacteristicItem(cdao.findOne(s.getTzx()));
			
			sdao.save(staff);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 查询所有数据
	 * @return
	 */
	public long findCount(){
		return sdao.count();
	}
	
	/**
	 * 分页查询所有
	 * @param page
	 * @param size
	 * @return
	 */
	public JSONArray  findStafflnform(){
		return sssd.findStafflnform();
	};
	//首页查询
	public List<ShouYeVo> querysy(Long stainId){
		List<Object[]> list = sdao.querysy(stainId);
		List<ShouYeVo> listvo=new ArrayList<>();
		if(list!=null){
			for(Object[] objs:list){
				StaffInformation s = (StaffInformation) objs[0];
				DefinitionDept d =(DefinitionDept) objs[1];
				ShouYeVo syvo = new ShouYeVo();
				syvo.setStainId(s.getStainId());
				syvo.setStainName(s.getStainName());
				syvo.setStainSex(s.getStainSex());
				syvo.setStainSrc(s.getStainSrc());
				if(d!=null){
					syvo.setDefideId(d.getDefideId());
					syvo.setDefideName(d.getDefideName());
				}
				
				listvo.add(syvo);
			}
		}
		
		return listvo;
	}
	
	public List<StaffInformation> findAll(int page,int rows){
	//	List<StaffVO> listvo = new ArrayList<>();
		List<StaffInformation> obj = sdao.queryAll(new PageRequest(page-1,rows));
		
		/*if(obj!=null){
			for (Object[] o : obj) {
				StaffVO vo = new StaffVO();
				StaffInformation s = (StaffInformation) o[0];
				vo.setStainId(s.getStainId());
				vo.setStainName(s.getStainName());
				vo.setStainSex(s.getStainSex());
				vo.setStainPolitics(s.getStainPolitics());
				vo.setStainDate(s.getStainDate());
				
				DefinitionDept d = (DefinitionDept) o[1];
				v7
				‘o.setDefideName(d.getDefideName());  3kjhgfd
				
				ProfessionalTitle p = (ProfessionalTitle) o[2];
				vo.setZcName(p.getZcName());
				
				Edubackground e = (Edubackground) o[3];
				vo.setEduName(e.getEduName());
				
				JobStatus j = (JobStatus) o[4];
				vo.setJobsName(j.getJobsName());
				
				LaborTurnover la = (LaborTurnover) o[5];
				vo.setLaborId(la.getLaborId());
				vo.setVoluntaryTurnover(la.getVoluntaryTurnover());
				if(vo.getVoluntaryTurnover().intValue()==0){
					listvo.add(vo);
				}
			}
		}*/
		return obj;
	}
	
	/**
	 * 模糊查询记录数
	 * @param name
	 * @return
	 */
	public Long findLikeCount(String name){
		return (long) sdao.queryLikeName("%"+name+"%",null).size();
	}
	/**
	 * 模糊查询所有
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<StaffInformation> findLikeAll(String name,int page,int rows){
		//List<StaffVO> listvo = new ArrayList<>();
		List<StaffInformation> obj = sdao.queryLikeName("%"+name+"%", new PageRequest(page-1,rows));
		/*if(obj!=null){
			for (Object[] o : obj) {
				StaffVO vo = new StaffVO();
				StaffInformation s = (StaffInformation) o[0];
				vo.setStainId(s.getStainId());
				vo.setStainName(s.getStainName());
				vo.setStainSex(s.getStainSex());
				vo.setStainPolitics(s.getStainPolitics());
				vo.setStainDate(s.getStainDate());
				
				DefinitionDept d = (DefinitionDept) o[1];
				vo.setDefideName(d.getDefideName());
				
				ProfessionalTitle p = (ProfessionalTitle) o[2];
				vo.setZcName(p.getZcName());
				
				Edubackground e = (Edubackground) o[3];
				vo.setEduName(e.getEduName());
				
				JobStatus j = (JobStatus) o[4];
				vo.setJobsName(j.getJobsName());
				
				LaborTurnover la = (LaborTurnover) o[5];
				vo.setLaborId(la.getLaborId());
				vo.setVoluntaryTurnover(la.getVoluntaryTurnover());
				if(vo.getVoluntaryTurnover().intValue()==0){
					listvo.add(vo);
				}
			}
		}*/
		return obj;
	}
	
	/**
	 * 查询员工数据
	 * @param page
	 * @param size
	 * @return
	 */
	public StaffInformation findOneEmp(Long sid){
		return sdao.findOne(sid);
	}
	/**
	 * 新增员工离职
	 */
	public void insertLaborTurnover(Long id,Long id2,Timestamp time,String person,String beizhu){
		StaffInformation staff = sdao.findOne(id);
		List<LaborTurnover> list = staff.getLaborTurnovers();
		LaborTurnover la = list.get(0);
		la.setVoluntaryTurnover(new BigDecimal(1));
		ReasonsLeaving re = reDao.findOne(id2);
		la.setReasonsLeaving(re);
		la.setDimissionDate(time);
		la.setExecutiveChampion(person);
		la.setLaborRemark(beizhu);
		la.setLaborFanping(new BigDecimal(0));
		laborDao.save(la);
	}
	
	/**
	 * 查询所有离职员工信息
	 * @return
	 */
	public JSONArray laborFindAll(){
		return laborMapper.findAll();
	}
	
	/**
	 * 查询所有离职员工信息
	 * @return
	 */
	public JSONArray laborLikeFindAll(String name){
		return laborMapper.LikeAll("%"+name+"%");
	}
	
	/**
	 * 查询所有返聘员工信息
	 * @return
	 */
	public JSONArray foldBackFindAll(){
		return laborMapper.foldBackAll();
	}
	
	/**
	 * 查询所有返聘员工信息
	 * @return
	 */
	public JSONArray foldLikeBackAll(String name){
		return laborMapper.foldLikeBackAll("%"+name+"%");
	}
	
	
	/**
	 * 改变离职状态
	 * @param id
	 */
	public void updateFoldBack(Long id){
		LaborTurnover la = laborDao.findOne(id);
		la.setVoluntaryTurnover(new BigDecimal(0));
		laborDao.save(la);
	}
}
